package cn.yjr.vectori.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import cn.yjr.vector.R;
import cn.yjr.vector.a.DaoMaster;
import cn.yjr.vector.a.DaoSession;
import cn.yjr.vector.a.Doctor;
import cn.yjr.vector.a.DoctorDao;
import cn.yjr.vector.a.My_Registration;
import cn.yjr.vector.a.My_RegistrationDao;
import cn.yjr.vector.a.DaoMaster.DevOpenHelper;
import cn.yjr.vector.a.My_RegistrationDao.Properties;
import cn.yjr.vector.tools.RegistrationInfo;
import de.greenrobot.dao.Property;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;

public class MyRegistrationAdapter extends BaseAdapter
  implements StickyListHeadersAdapter
{
  public static List<RegistrationInfo> contents;
  Context context;
  private Cursor cursor;
  private DaoMaster daoMaster;
  private DaoSession daoSession;
  private SQLiteDatabase db;
  private DoctorDao doctorDao;
  DaoMaster.DevOpenHelper helper;
  LayoutInflater inflater;
  private My_RegistrationDao myRegistrationDao;
  String[] weekName = {"星期日","星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

  public MyRegistrationAdapter(Context paramContext)
  {
    this.context = paramContext;
    this.inflater = LayoutInflater.from(paramContext);
    contents = new ArrayList<RegistrationInfo>();
    this.helper = new DaoMaster.DevOpenHelper(paramContext, "yijiaren-db", null);
    this.db = this.helper.getWritableDatabase();
    this.daoMaster = new DaoMaster(this.db);
    this.daoSession = this.daoMaster.newSession();
    this.myRegistrationDao = this.daoSession.getMy_RegistrationDao();
    this.doctorDao = this.daoSession.getDoctorDao();
    initRegistrationInfo();
  }

  private void addInfo(Date paramDate)
  {
    My_Registration localMy_Registration = new My_Registration(null, paramDate, "123456", 1L);
    this.myRegistrationDao.insert(localMy_Registration);
  }

  @SuppressLint({"SimpleDateFormat"})
  private void initRegistrationInfo()
  {
    Doctor localDoctor = new Doctor(null, 2L, "柳熠", "北医三院", "神经科", "主治医师");
    this.doctorDao.insert(localDoctor);
    addInfo(new Date());
    addInfo(new Date());
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    
    try {
		addInfo(localSimpleDateFormat.parse("2013-12-20 13:54"));
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    String str = My_RegistrationDao.Properties.Date.columnName + " desc";
    this.cursor = this.db.query(this.myRegistrationDao.getTablename(), this.myRegistrationDao.getAllColumns(), null, null, null, null, str);
    List<My_Registration> registrations = this.myRegistrationDao.loadAllDeepFromCursor(this.cursor);
    for(int i = 0; i < registrations.size(); i++){
    	RegistrationInfo tmp = ((My_Registration)registrations.get(i)).switchTo();
    	contents.add(tmp);
    	contents.add(tmp);
    }

  }

  public int getCount()
  {
    return contents.size();
  }

  public long getHeaderId(int paramInt)
  {
    return ((RegistrationInfo)contents.get(paramInt)).getId();
  }

  public View getHeaderView(int paramInt, View paramView, ViewGroup paramViewGroup)
  {
    HeaderViewHolder localHeaderViewHolder;
    if (paramView == null)
    {
      localHeaderViewHolder = new HeaderViewHolder();
      paramView = this.inflater.inflate(R.layout.my_registration_head, paramViewGroup, false);
      localHeaderViewHolder.year_month_tv = ((TextView)paramView.findViewById(R.id.year_month));
      paramView.setTag(localHeaderViewHolder);
    }else{
      localHeaderViewHolder = (HeaderViewHolder)paramView.getTag();
      RegistrationInfo localRegistrationInfo = (RegistrationInfo)contents.get(paramInt);
      localHeaderViewHolder.year_month_tv.setText(localRegistrationInfo.getYearMonth());
    }
    return paramView;
  }

  public Object getItem(int paramInt)
  {
    return contents.get(paramInt);
  }

  public long getItemId(int paramInt)
  {
    return paramInt;
  }

  public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
  {
    ViewHolder localViewHolder;
    if (paramView == null)
    {
      localViewHolder = new ViewHolder();
      paramView = this.inflater.inflate(R.layout.my_registration_item, paramViewGroup, false);
      localViewHolder.doc_info_tv = ((TextView)paramView.findViewById(R.id.doc_info));
      localViewHolder.time_info_tv = ((TextView)paramView.findViewById(R.id.time_info));
      localViewHolder.left_time_tv = ((TextView)paramView.findViewById(R.id.left_time));
      paramView.setTag(localViewHolder);
      RegistrationInfo localRegistrationInfo = (RegistrationInfo)contents.get(paramInt);
      localViewHolder.doc_info_tv.setText(localRegistrationInfo.getDocInfo());
      localViewHolder.time_info_tv.setText(localRegistrationInfo.getTime());
      if (!localRegistrationInfo.isEnd())
    	  localViewHolder.left_time_tv.setText("剩余3天");
      localViewHolder.left_time_tv.setText(localRegistrationInfo.getLeftTime());
    }else{
      localViewHolder = (ViewHolder)paramView.getTag();
    }
    return paramView;
  }

  class HeaderViewHolder
  {
    TextView year_month_tv;

    HeaderViewHolder()
    {
    }
  }

  class ViewHolder
  {
    TextView doc_info_tv;
    TextView left_time_tv;
    TextView time_info_tv;

    ViewHolder()
    {
    }
  }
}